<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class User_model extends CI_Model {

	public function get_list($id = false){
		$this->db->from('users a');
		$this->db->join('ref_level b','a.level = b.lv_id','left');
		$this->db->where('id_user > 1');
		if($id) $this->db->where('a.id_user',$id);
		return $this->db->get();
	}

	public function get_data($id = false, $code = false, $table = false){
		$this->db->from('users a');
		$this->db->join($table.' b','a.id_user = b.'.$code.'id_user','left');
		$this->db->where('a.id_user',$id);
		return $this->db->get();
	}

	public function do_add($param = false){
		if($param){
			if($param['password']){
				$password = md5($param['password']);
				$this->db->set('password',$password);
				$this->db->set('id_session',$password);
				unset($param['password']);
			}
			$this->db->set('username',$param['username']);
			$this->db->set('level',$param['group']);
			$this->db->insert('users');
			// $id = $this->db->insert_id();
			return true;
		}
		return false;
	}

	public function do_add_profile($table = false, $param = false){
		if($param){			
			return $this->db->insert($table,$param);
		}
		return false;
	}

	public function do_update($id = false,$param = false){
		if($id){
			if($param['password']){
				$password = md5($param['password']);
				$this->db->set('password',$password);
				$this->db->set('id_session',$password);
				unset($param['password']);
			}else{
				unset($param['password']);
			}
			$this->db->where('id_user',$id);
			return $this->db->update('users',$param);
		}
		return false;
	}

	public function do_update_profile($id = false,$table = false,$code = false,$param = false){
		if($id){			
			$this->db->where($code.'id',$id);
			return $this->db->update($table,$param);
		}
		return false;
	}

	public function do_delete($id = false){
		if($id){
			$this->db->where('id_user',$id);
			return $this->db->delete('users');
		}return false;
	}

	public function get_level($id = false){
		if($id) $this->db->where('lv_id',$id);
		return $this->db->get('ref_level');
	}
}